package com.example.object.screen;

import android.content.Context;
import android.util.Log;

import com.example.bounce.R;
import com.example.globject.GLSquare;
import com.example.object.GameShape;
import com.example.shape.Square;
import com.example.state.AppState;

import java.util.Random;

/**
 * Created by Lucas on 06/06/13.
 */
public class StarShape extends GameShape {

    /**
     * The glObject of the star
     */
    private GLSquare _star;
    /**
     * The glObject of the ring's star
     */
    private GLSquare _starRing;
    /**
     * The square used to calculate the next coordinate
     */
    private Square _square;
    /**
     * The debug TAG
     */
    private final String TAG = "StarShape";
    /**
     * The direction on the x axis
     */
    private float _xDirection;
    /**
     * The direction on the y axis
     */
    private float _yDirection;
    /**
     * The angle of rotation
     */
    private float _angle;

    /**
     * The constructor
     * @param context The context of the application
     * @param xCenter the x coordinate of the origin
     * @param yCenter the y coordinate of the origin
     * @param halfSize the size of the star
     */
    public StarShape(Context context, float xCenter, float yCenter, float halfSize){
        Log.i(TAG, "StarShape constructor");
        _handlerInitiate = false;
        _userData = AppState.NULL;
        _star = new GLSquare(context, xCenter, yCenter, halfSize, halfSize, R.raw.star);
        _starRing = new GLSquare(context, xCenter, yCenter, halfSize, halfSize, R.raw.starring);
        _square = new Square(xCenter, yCenter, halfSize, halfSize);

        Random r = new Random();
        _xDirection = ((r.nextInt(2))+1)/150f;
        _yDirection = ((r.nextInt(2))+1)/150f;
        _angle = (r.nextInt(2))+1;
    }

    /**
     * Return that the star object is movable
     * @return true
     */
    @Override
    public boolean isMovable() {
        return true;
    }

    /**
     * Return that the star object is rotatable
     * @return true
     */
    @Override
    public boolean isRotatable() {
        return true;
    }

    /**
     * Rotate the star object according to its angle
     */
    @Override
    public void rotate() {
        _star.rotate(_angle);
        _starRing.rotate(-_angle);
    }

    /**
     * Move the star according to its direction
     * @param ofX
     * @param ofY
     */
    @Override
    public void moveOf(float ofX, float ofY){

        if (_square.getXCenter()-_square.getMidWidth()+_yDirection < 0 ||
                _square.getXCenter()+_square.getMidWidth()+_yDirection  > AppState.RATIOHEIGHT*2f){
            _square.moveOf(-_xDirection, 0);
            _star.moveOf(-_xDirection, 0);
            _starRing.moveOf(-_xDirection, 0);
            changeXDirection();
        }
        if (_square.getYCenter()+_square.getMidHeight()+_yDirection > 2f ||
                _square.getYCenter()-_square.getMidHeight()+_yDirection < 0){
            _square.moveOf(0, -_yDirection);
            _star.moveOf(0, -_yDirection);
            _starRing.moveOf(0, -_yDirection);
            changeYDirection();
        }
        _square.moveOf(_xDirection, _yDirection);
        _star.moveOf(_xDirection, _yDirection);
        _starRing.moveOf(_xDirection, _yDirection);
    }

    /**
     * once the star touch the border of the screen it changes direction
     */
    private void changeXDirection() {
        Random r = new Random();
        if(_xDirection<0){
            _xDirection = (r.nextInt(2)+1)/150f;
        }else {
            _xDirection = ((r.nextInt(2)+1)/150f)*(-1);
        }
    }

    /**
     * once the star touch the border of the screen it changes direction
     */
    private void changeYDirection() {
        Random r = new Random();
        if(_yDirection<0){
            _yDirection = ((r.nextInt(2))+1)/100f;
        }else {
            _yDirection = ((r.nextInt(2)+1)/100f)*(-1);
        }
    }

    /**
     * Draw the shape into the GLSurfaceScreen
     */
    @Override
    public void draw() {
        _starRing.draw();
        _star.draw();
    }

    /**
     * set the OpenGL resource to the openGL Object
     *
     * @param aPositionHandle
     * @param aTextureHandle
     * @param uMVPMatrixHandle
     * @param vMatrix
     * @param projMatrix
     */
    @Override
    public void setGLHandler(int aPositionHandle, int aTextureHandle, int uMVPMatrixHandle, float[] vMatrix, float[] projMatrix) {
        _star.setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix, null);
        _starRing.setGLHandler(aPositionHandle, aTextureHandle, uMVPMatrixHandle, vMatrix, projMatrix, null);
        _handlerInitiate = true;
    }

    public String toString(){
        return "StarShape";
    }
}
